<?php
define('ROOT',dirname(dirname(__FILE__)));
define('CONTROL',ROOT."/control");
define('MODEL',ROOT."/model");
define('DATAMODEL',ROOT."/datamodel");
define('VIEW',ROOT."/view");
define('LIB',ROOT."/lib");
define('COMMON',ROOT."/common");
define('CONFIG',ROOT."/config");
define('ATTACH',ROOT."/attach");
define('PICUP',ROOT."/picup");
define('ERRORLOG',ROOT."/log");
function import($dir)
{
	$dir_Arr = explode(".",$dir);
	$className = end($dir_Arr);
	if(!class_exists($className))
	{
		//echo LIBS .'/'. strtr($dir,array('.'=>'/')).".php"; echo "<hr>";
		require_once (LIBS .'/'. strtr($dir,array('.'=>'/')).".php") ;		
	}
}
function _mvc_load($classname) {
    $tihuanArr = array('new_'=>'new/');
    $classname = strtr($classname,$tihuanArr);
	$ext = '.class.php';
	if(!class_exists($classname))
	{
		if (preg_match ( '/Control$/', $classname )) 
		{
			//echo CONTROL .'/'. $classname . $ext;
			require_once (CONTROL .'/'. $classname . $ext) ;		
		} elseif (preg_match ( '/Model$/', $classname )) 
		{
			//echo MODEL .'/'. $classname . $ext; echo "<hr>";
			require_once (MODEL .'/'. $classname . $ext);
		} elseif (preg_match ( '/Data$/', $classname )) 
		{
			require_once (DATAMODEL . '/' . $classname . $ext);
		}else
		{
			
		}
	}	
}
function getDB($classname='DBData') {
	$ext = '.class.php';
	if(!class_exists($classname))
	{
//		echo DATAMODEL . '/' . $classname . $ext; exit();
		require_once (DATAMODEL . '/' . $classname . $ext);
	}
}
function geturlbaseArr()
{
    $rURI = $_SERVER['REQUEST_URI'];
    if($rURI == '/')
    {
        $rURI = '/index/index';
    }
    $qString = '';
    if (strpos($rURI, '?') !== false)
    {
        $rURIs = explode('?', $rURI);
        $rURI = $rURIs[0];
        $qString = $rURIs[1];
        unset($rURIs);
    }
    if(strtolower((substr($rURI, -5))) == '.html')
    {
        $rURI = substr($rURI, 0, strlen($rURI) - 5);
    }
	if(strtolower((substr($rURI, -4))) == '.php')
    {
        $rURI = substr($rURI, 0, strlen($rURI) - 4);
    }
    if (strpos($rURI, '-') !== false)
    {
        $rURIs = explode('-', $rURI);
        $rURI1_arr = array_filter( explode("/",$rURIs[0]) );
        $count = count($rURI1_arr);
        if($count==1)
        {
            $rURIs[0] = $rURIs[0]."/index";
        }
        $rURI = implode('-',$rURIs);
        unset($rURIs);
        unset($rURI1_arr);
    }
    $rURI = strtr($rURI,'-','/');
    if (substr_count($rURI, '/') == 1) {
        $rURI = $rURI.'/index';
    }
    $baseUrl_Arr = explode('/',$rURI);
    if(empty($baseUrl_Arr[2]))
    {
        $baseUrl_Arr[2] = 'index';
    }
    return array_filter($baseUrl_Arr);
}
function getconfig($arrname,$type = "arr")
{
	
	//$arrname = strtr($arrname,array('.'=>'/'));
	//echo  CONFIG.'/'.$arrname.'.php';
	$filename = CONFIG.'/'.$arrname.'.php';
	switch ($type)
	{
		case "arr":
			return include $filename;
			break;
		case "json":
			$json = include $filename;
			$arr = json_decode($json, true);
			return $arr;
			break;
	}
	
}
spl_autoload_register('_mvc_load');

?>